import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_wan_android/generated/l10n.dart';
import 'package:flutter_wan_android/manager/provider_manager.dart';
import 'package:flutter_wan_android/manager/router_manger.dart';
import 'package:flutter_wan_android/manager/storage_manager.dart';
import 'package:flutter_wan_android/view_model/locale_model.dart';
import 'package:flutter_wan_android/view_model/theme_model.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

///默认App的启动
class DefaultApp {
  ///运行app
  static Future<void> run() async {
    WidgetsFlutterBinding.ensureInitialized();

    ///初始化存储相关代码
    await StorageManager.init();

    ///加载页面
    runApp(MyApp());

    /// Android状态栏透明 splash为白色,所以调整状态栏文字为黑色
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarBrightness: Brightness.light));

    ///初始化参数
    initApp();
  }

  ///程序初始化操作
  static void initApp() {}
}

///
/// 保存全局Context
///
final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: providers,
        child: Consumer2<ThemeModel, LocaleModel>(
            builder: (context, themeModel, localeModel, child) {
          return RefreshConfiguration(
            hideFooterWhenNotFull: false,///列表数据不满一页,不触发加载更多
            child: MaterialApp(
              navigatorKey: navigatorKey,
              ///设置主题
              theme: themeModel.themeData(),
              darkTheme: themeModel.themeData(platformDarkMode: true),

              ///设置本地化
              locale: localeModel.locale,
              localizationsDelegates: const [
                RefreshLocalizations.delegate,
                S.delegate,
                GlobalCupertinoLocalizations.delegate,
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate
              ],
              supportedLocales: S.delegate.supportedLocales,

              ///设置路由
              onGenerateRoute: RoutesManager.getInstance().getRouterGenerator(),
              initialRoute: RoutesManager.indexPage,
            ),
          );
        }));
  }
}
